1 package uk.ac.roe.antigen.ant; 2 3 import java.awt.Dimension; 4 import java.awt.GridBagLayout; 5 import java.awt.GridBagConstraints; 6 import java.awt.LayoutManager; 7 import java.awt.Toolkit; 8 9 import java.awt.event.ActionListener; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.KeyAdapter; 12 import java.awt.event.KeyEvent; 13 import java.util.Enumeration; 14 import java.util.Hashtable; 15 16 import javax.swing.JButton; 17 import javax.swing.JDialog; 18 import javax.swing.JTextField; 19 import javax.swing.JLabel; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.ProjectComponent; 24 import org.apache.tools.ant.Target; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.input.DefaultInputHandler; 27 import org.apache.tools.ant.input.InputRequest; 28 import org.apache.tools.ant.input.InputHandler; 29 import org.apache.tools.ant.input.MultipleChoiceInputRequest; 30 31 32 public class PlainTextGUIInputHandler 33 extends DefaultInputHandler { 34 /*** 35 * The JDialog that contains the component in which the input 36 * is typed. 37 */ 38 protected JDialog dlgInput; 39 40 /*** 41 * The layout of the dialog. 42 */ 43 protected LayoutManager layout; 44 45 /*** 46 * The label of the input component. 47 */ 48 protected JLabel lblInput; 49 50 /*** 51 * The JComponent where the input is typed. 52 */ 53 protected JTextField fldInput; 54 55 /*** 56 * The JButton that will passed the input to the input request. 57 */ 58 protected JButton btnSubmit; 59 60 /*** 61 * Default no-arg constructor. 62 * Initializes the graphiacal components. 63 */ 64 public PlainTextGUIInputHandler() { 65 dlgInput = new JDialog(); 66 dlgInput.setModal(true); 67 layout = new GridBagLayout(); 68 lblInput = new JLabel(); 69 fldInput = new JTextField(); 70 btnSubmit = new JButton(); 71 } 72 73 74 protected void addWidgets() { 75 dlgInput.getContentPane().setLayout(layout); 76 77 GridBagConstraints gbc = new GridBagConstraints(); 78 gbc.gridx = 0; 79 gbc.gridy = 0; 80 gbc.anchor = GridBagConstraints.WEST; 81 gbc.insets.left = 5; 82 gbc.insets.right = 5; 83 gbc.insets.bottom = 5; 84 gbc.insets.top = 5; 85 dlgInput.getContentPane().add(lblInput, gbc); 86 87 gbc.gridx = 1; 88 gbc.insets.bottom = 0; 89 gbc.insets.top = 0; 90 gbc.fill = GridBagConstraints.HORIZONTAL; 91 dlgInput.getContentPane().add(fldInput, gbc); 92 93 gbc.gridx = 0; 94 gbc.gridy = 1; 95 gbc.gridwidth = 2; 96 gbc.fill = GridBagConstraints.NONE; 97 gbc.anchor = GridBagConstraints.CENTER; 98 gbc.insets.bottom = 5; 99 dlgInput.getContentPane().add(btnSubmit, gbc); 100 } 101 102 public void handleInput(InputRequest request) { 103 addWidgets(); 104 setEventListeners(request); 105 106 107 108 109 dlgInput.setTitle(getPrompt(request)); 110 lblInput.setText(getPrompt(request)); 111 fldInput.setPreferredSize(lblInput.getPreferredSize()); 112 fldInput.setText(getDefault(request)); 113 btnSubmit.setText("Submit"); 114 115 if (request instanceof MultipleChoiceInputRequest) { 116 btnSubmit.setText("Multiple choice"); 117 } 118 119 dlgInput.pack(); 120 dlgInput.setLocation(getDialogLocation().width, getDialogLocation().height); 121 dlgInput.show(); 122 } 123 124 /*** 125 * @param request 126 * @return 127 */ 128 private String getDefault(InputRequest request) { 129 return request.getDefault(); 130 } 131 132 133 /*** 134 * Sets AWT Listeners to the components which will recieve or 135 * submit the events. 136 * 137 * @param request the InputRequest object, where the contents 138 * of the input component will be set. 139 */ 140 protected void setEventListeners(final InputRequest request) { 141 btnSubmit.addActionListener( 142 new ActionListener() { 143 public void actionPerformed(ActionEvent e) { 144 request.setInput(fldInput.getText()); 145 dlgInput.dispose(); 146 } 147 }); 148 149 fldInput.addKeyListener( 150 new KeyAdapter() { 151 public void keyPressed(KeyEvent e) { 152 if (e.getKeyCode() == KeyEvent.VK_ENTER) { 153 request.setInput(fldInput.getText()); 154 dlgInput.dispose(); 155 } 156 } 157 }); 158 } 159 160 /*** 161 * Calculates the desired location of the dialog on the screen. 162 * By default it is in the center of the screen. 163 * 164 * @return the desired location of the dialog on the screen 165 */ 166 protected Dimension getDialogLocation() { 167 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 168 Dimension dlgInputDim = dlgInput.getSize(); 169 int dlgInputX = (int) 170 ((screenDim.getWidth() - dlgInputDim.getWidth()) / 2); 171 int dlgInputY = (int) 172 ((screenDim.getHeight() - dlgInputDim.getHeight()) / 2); 173 return new Dimension(dlgInputX, dlgInputY); 174 } 175 176 }